﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HATerrain;
using HA.Data;
using System.IO;

namespace HA.ModFlow.Controls.Windows.DataOperation
{
    /// <summary>
    /// winDemImporter.xaml 的交互逻辑
    /// </summary>
    public partial class winDemImporter : Window
    {
        public winDemImporter(Project proj,TerrainData data,string fileName)
        {
            terrainData = data;
            project = proj;
            InitializeComponent();

            try
            {
                switch (terrainData.Header.Unit)
                {
                    case TerrainUnitEnum.radians:
                        this.txtBoxUint.Text = "经纬度（度）";
                        break;
                    case TerrainUnitEnum.feet:
                        this.txtBoxUint.Text = "英尺";
                        break;
                    case TerrainUnitEnum.meters:
                        this.txtBoxUint.Text = "米";
                        break;
                    case TerrainUnitEnum.arcseconds:
                        this.txtBoxUint.Text = "经纬度（秒）";
                        break;
                    default:
                        break;
                }

                this.txtBoxName.Text = fileName;
                this.txtBoxMaxValue.Text = terrainData.Header.DataMax.ToString();
                this.txtBoxMinValue.Text = terrainData.Header.DataMin.ToString();
                this.txtBoxNotValidValue.Text = terrainData.Header.NoDataValue.ToString();
                this.txtBlockProjection.Text = terrainData.Header.Projection;
                this.txtBoxSWCoordinate.Text = terrainData.Header.CoordinateSW.X.ToString("F5") + ", " + this.terrainData.Header.CoordinateSW.Y.ToString("F5");
                this.txtXOffSet.DataContext = this;
                this.txtYOffSet.DataContext = this;
                this.slider1.DataContext = this;
                this.txtDensity.DataContext = this;
                this.slider1.Value = 1;
            }
            catch
            {
                terrainData = null;
                this.Close();
            }

        }

        Project project;
        TerrainData terrainData;

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            if (terrainData != null && this.Density != 0)
            {
                Scatter2D s2D = project.Scatter2dData.CreateNew();
                if (this.txtBoxName.Text.Trim() != "" && !s2D.ValueNames.Contains(this.txtBoxName.Text))
                    s2D.ValueNames.Add(txtBoxName.Text);
                else
                    s2D.ValueNames.Add(s2D.Key.ToString());
                double orginX = terrainData.Header.CoordinateNW.X;
                double orginY = terrainData.Header.CoordinateNW.Y;

                HAPoint vectorPoint = terrainData.Header.CoordinateNE - terrainData.Header.CoordinateNW;
                Vector vectRow = new Vector(vectorPoint.X, vectorPoint.Y) / terrainData.Header.SizeX;
                vectorPoint = terrainData.Header.CoordinateSE - terrainData.Header.CoordinateNE;
                Vector vectCol = new Vector(vectorPoint.X, vectorPoint.Y) / terrainData.Header.SizeY;

                int len0 = terrainData.Data2D.GetLength(0);
                int len1 = terrainData.Data2D.GetLength(1);



                double slen0 = 1 / Density;
                double slen1 = 1 / Density;

                for (double i = 0; i < len0; i += slen0)
                {
                    double offX = orginX + _xOffSet + vectCol.X * (int)i;
                    double offY = orginY + _yOffSet + vectCol.Y * (int)i;

                    for (double j = 0; j < len1; j += slen1)
                    {
                        if (terrainData.Data2D[(int)i, (int)j] == terrainData.Header.NoDataValue) continue;
                        s2D.DataSet.Add(new ScatterPoint(s2D.GetNextScatterPointId(), new List<double> { terrainData.Data2D[(int)i, (int)j] }, offX + vectRow.X * (int)j, offY + vectRow.Y * (int)j));
                    }
                    if ((len1 - 1) % slen1 != 0)
                    {
                        s2D.DataSet.Add(new ScatterPoint(s2D.GetNextScatterPointId(), new List<double> { terrainData.Data2D[(int)i, len1 - 1] }, offX + vectRow.X * (len1 - 1), offY + vectRow.Y * (len1 - 1)));
                    }
                }
                if ((len0 - 1) % slen0 != 0)
                {
                    double offX = orginX + _xOffSet + vectCol.X * (len0 - 1);
                    double offY = orginY + _yOffSet + vectCol.Y * (len0 - 1);

                    for (double j = 0; j < len1; j += slen1)
                    {
                        if (terrainData.Data2D[len0 - 1, (int)j] == terrainData.Header.NoDataValue) continue;
                        s2D.DataSet.Add(new ScatterPoint(s2D.GetNextScatterPointId(), new List<double> { terrainData.Data2D[len0 - 1, (int)j] }, offX + vectRow.X * (int)j, offY + vectRow.Y * (int)j));
                    }
                    if ((len1 - 1) % slen1 != 0)
                    {
                        s2D.DataSet.Add(new ScatterPoint(s2D.GetNextScatterPointId(), new List<double> { terrainData.Data2D[len0 - 1, len1 - 1] }, offX + vectRow.X * (len1 - 1), offY + vectRow.Y * (len1 - 1)));
                    }
                }

                project.Scatter2dData.Add(s2D);
                 
            }

            this.DialogResult = true;
            this.Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        #region 属性

        /// <summary>
        /// 取点密度
        /// </summary>
        public double Density
        {
            get { return Convert.ToDouble(this.txtDensity.Text); }
        }

        double _xOffSet = 0;
        /// <summary>
        /// x方向偏移量
        /// </summary>
        public double XOffSet
        {
            get { return _xOffSet; }
            set { _xOffSet = value; }
        }

        double _yOffSet = 0;
        /// <summary>
        /// y方向偏移量
        /// </summary>
        public double YOffSet
        {
            get { return _yOffSet; }
            set { _yOffSet = value; }
        }

        #endregion
    }

    public class ConvertorDensity : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double density = (double)value + 0.0;

            return density.ToString("F2");
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                return System.Convert.ToDouble(value);
            }
            catch
            {
                return 1;
            }
        }
    }
}
